package com.lych.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.lych.model.mongo.Visitors;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class VisitorsApiImpl implements VisitorsApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存访客数据
     * 对于一个用户，一天只保存一次
     * @param visitors
     */
    @Override
    public void save(Visitors visitors) {
        Query query = Query.query(Criteria.where("userId").is(visitors.getUserId())
                .and("visitorUserId").is(visitors.getVisitorUserId())
                .and("visitDate").is(visitors.getVisitDate()));
        if (!mongoTemplate.exists(query,Visitors.class) && visitors.getUserId() != visitors.getVisitorUserId()){
            mongoTemplate.save(visitors);
        }
    }

    /**
     * 得到访客id
     *
     * @param date
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public List<Long> findByUserId(Long date, Long userId, Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("date").gt(date))
                .skip((page - 1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("date")));
        List<Visitors> visitors = mongoTemplate.find(query, Visitors.class);
        return CollUtil.getFieldValues(visitors,"visitorUserId",Long.class);
    }

    /**
     * 查询最后一人访问的时间
     * @param aLong
     * @param userId
     * @return
     */
    @Override
    public Visitors findById(Long aLong, Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("visitorUserId").is(aLong))
                .with(Sort.by(Sort.Order.desc("date")))
                .limit(1);
        return mongoTemplate.findOne(query,Visitors.class);
    }

    /**
     * 查询首页访客列表
     * @param date
     * @param userId
     * @return
     */
    @Override
    public List<Visitors> visitors(Long date, Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId);
        if (date != null){
            criteria.and("date").gt(date);
        }
        Query query = Query.query(criteria).limit(5).with(Sort.by(Sort.Order.desc("date")));
        return mongoTemplate.find(query,Visitors.class);
    }

    /**
     * 根据id查询缘分值
     *
     * @param userId
     * @param fallInLoves
     * @return
     */
    @Override
    public List<Visitors> findScoreByIds(Long userId, List<Long> fallInLoves) {
        Query query = Query.query(Criteria.where("visitorUserId").in(fallInLoves)
                .and("userId").is(userId));
        return mongoTemplate.find(query,Visitors.class);
    }

}
